package com.fitness.fitness.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fitness.fitness.convertor.CoursePlanConvertor;
import com.fitness.fitness.mapper.CourseMapper;
import com.fitness.fitness.mapper.CoursePlanMapper;
import com.fitness.fitness.modal.course.dto.CoursePlanDto;
import com.fitness.fitness.modal.course.entity.Course;
import com.fitness.fitness.modal.course.entity.CoursePlan;
import com.fitness.fitness.modal.course.params.CoursePlanPageQueryParams;
import com.fitness.fitness.modal.course.vo.CoursePlanVO;
import com.fitness.fitness.service.ICoursePlanService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CoursePlanServiceImpl extends ServiceImpl<CoursePlanMapper,CoursePlan> implements ICoursePlanService {

    private final CoursePlanConvertor coursePlanConvertor;
    private final CourseMapper courseMapper;

    @Override
    public Page<CoursePlanVO> getCoursePlanPage(CoursePlanPageQueryParams queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<CoursePlan> page = new Page<>(pageNum, pageSize);

        // 查询数据
        Page<CoursePlan> coachBoPage = this.baseMapper.getCoursePlanPage(page, queryParams);

        return coursePlanConvertor.entity2Vo(coachBoPage);
    }

    @Override
    public boolean addCoursePlan(CoursePlanDto equipmentDto) {
        return save(coursePlanConvertor.dto2Entity(equipmentDto));
    }

    @Override
    public boolean updateCoursePlan(CoursePlanDto equipmentDto) {
        return updateById(coursePlanConvertor.dto2Entity(equipmentDto));
    }

    @Override
    public List<CoursePlanVO> getCourPlansVOByCourseId(Long id) {
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.eq("id", id);
        final Course course = courseMapper.selectOne(courseQueryWrapper);
        QueryWrapper<CoursePlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", id);
        List<CoursePlan> plans = this.baseMapper.selectList(queryWrapper);
        List<CoursePlanVO> coursePlanVOS = coursePlanConvertor.entities2Vos(plans);
        coursePlanVOS.forEach(coursePlanVO -> {
            coursePlanVO.setCourseName(course.getName());
        });
        return coursePlanVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAdd(List<CoursePlanDto> planDtos) {
        Long courseId = planDtos.get(0).getCourseId();
        Course course = courseMapper.selectById(courseId);
        if(course == null){
            return false;
        }
        QueryWrapper<CoursePlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        this.baseMapper.delete(queryWrapper);
        List<CoursePlan> coursePlans = coursePlanConvertor.dtos2Entities(planDtos);
        this.baseMapper.insertBatchSomeColumn(coursePlans);
        return true;
    }
}
